package org.bizming.web.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionContext;

public class WebUtil {
	/** 为ajax请求封装json数据 */
	public static void outputJSONResult(String jsonData,
			HttpServletResponse response) {

		try {
			response.setHeader("ContentType", "text/json;charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			response.setCharacterEncoding("utf-8");
			PrintWriter pw = response.getWriter();

			pw.write(jsonData);
			pw.flush();
			pw.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/** 为ajax请求封装json数据 */
	public static void outputJSONResult(JSONObject object,
			HttpServletResponse response) {

		String result = object.toJSONString();
		outputJSONResult(result, response);
	}

	/** 获取HttpServletRequest */
	public static final HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/** 获取HttpServletResponse */
	public static final HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/** 返回session */
	public static final Map<String, Object> getSession() {
		return ActionContext.getContext().getSession();
	}

}
